import { getServer } from '/store/mutations'
import { currencyApi } from '/api/currency'
import { userApi } from '/api/user'

Page({
  data: {
    user: {},
    limit: 15,
    loading: false,
    /** 收入 */
    data_0: {
      canLoad: true,
      data: [],
    },
    // 提现
    data_1: {
      canLoad: true,
      data: [],
    },
    price: '',
    currentIndex: 0,
    showTip: false,
    // 滑动区域的高度
    scrollHeight: 1000,
  },
  async onLoad() {
    const user = wx.getStorageSync(`${getServer()}/logged`).data
    this.setData({
      user,
    })
    this.feachData()
  },
  onReady() {
    const query = wx.createSelectorQuery()
    query.select('#header').boundingClientRect()
    query.select('#tab').boundingClientRect()
    query.exec((res) => {
      // 分别取出节点的高度
      const headerHeight = res[0].height
      const tabHeight = res[1].height
      // 然后窗口高度（wx.getSystemInfoSync().windowHeight）减去其他不滑动界面的高度
      const scrollViewHeight = wx.getSystemInfoSync().windowHeight - headerHeight - tabHeight
      this.setData({
        scrollHeight: scrollViewHeight,
      })
    })
  },
  onReachBottom() {
    this.feachData(true)
  },
  // 提现
  goWithDraw() {
    wx.navigateTo({
      url: '/mine/withdraw/withdraw',
    })
  },
  closeShadow() {
    if (this.data.showTip) {
      this.setData({
        showTip: false,
      })
      return
    }
  },
  // 显示冻结提示
  showFrozeTip() {
    this.setData({
      showTip: true,
    })
  },
  titleClick(e: any) {
    const { position } = e.currentTarget.dataset
    this.setData({
      currentIndex: position,
    })
    const {
      data: {
        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
        // @ts-ignore
        [`data_${this.data.currentIndex}`]: { data },
      },
    } = this
    // 有数据就不加载
    if (data.length == 0) {
      this.feachData()
    }
  },
  toDetail(e: any) {
    if (this.data.showTip) {
      this.setData({
        showTip: false,
      })
      return
    }
  },
  feachData(loadmore = false) {
    const {
      data: { currentIndex, loading, limit },
    } = this
    if (loading) return
    // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
    // @ts-ignore
    const {
      data: {
        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
        // @ts-ignore
        [`data_${currentIndex}`]: { canLoad, data },
      },
    } = this
    if (!canLoad) return
    this.setData({ loading: true })
    const after = loadmore && data[data.length - 1] ? data[data.length - 1].id : 0

    if (currentIndex == 0) {
      const action = 'income'
      // 收入
      currencyApi
        .walletIncomeOrder({ after, limit, action })
        .then((data) => {
          this.setData({
            loading: false,
            [`data_${currentIndex}_loaded`]: true,
            [`data_${currentIndex}`]: {
              // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
              // @ts-ignore
              data: loadmore ? [...this.data[`data_${currentIndex}`].data, ...data] : data,
              canLoad: data.length === limit,
            },
          })
        })
        .catch(() => {
          this.setData({
            loading: false,
          })
        })
    } else {
      userApi
        .withdrawList({ limit, after })
        .then((data) => {
          this.setData({
            loading: false,
            [`data_${currentIndex}_loaded`]: true,
            [`data_${currentIndex}`]: {
              // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
              // @ts-ignore
              data: loadmore ? [...this.data[`data_${currentIndex}`].data, ...data] : data,
              canLoad: data.length === limit,
            },
          })
        })
        .catch(() => {
          this.setData({
            loading: false,
          })
        })
    }
  },
})
